﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
Imports System.Globalization
Imports System.IO.IsolatedStorage

Namespace Pages

    Partial Public Class RegisterPage
        Inherits PhoneApplicationPage

        Private Const NameClaimType As String = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
        Private Const EmailClaimType As String = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"

        Public Sub New()
            Me.InitializeComponent()

            Me.ViewModel = New RegisterPageViewModel()
            Dim items = ParseQueryString(App.CloudClientFactory.TokenStore.SecurityToken)
            Dim claimsUserName = items(HttpUtility.UrlEncode(NameClaimType))
            Dim claimsEmail = items(HttpUtility.UrlEncode(EmailClaimType))
            Me.ViewModel.UserName = If(String.IsNullOrEmpty(claimsUserName), String.Empty, claimsUserName)
            Me.ViewModel.EMail = If(String.IsNullOrEmpty(claimsEmail), String.Empty, claimsEmail)
        End Sub

        Public Property ViewModel() As RegisterPageViewModel
            Get
                Return TryCast(Me.DataContext, RegisterPageViewModel)
            End Get
            Set(ByVal value As RegisterPageViewModel)
                Me.DataContext = value
            End Set
        End Property

        Private Shared Sub SetIsolatedStorageSetting(ByVal key As String, ByVal value As Object)
            Dim isolatedStorage As IDictionary(Of String, Object) = IsolatedStorageSettings.ApplicationSettings
            If isolatedStorage.ContainsKey(key) Then
                isolatedStorage.Remove(key)
            End If

            isolatedStorage.Add(key, value)
        End Sub

        Private Shared Function ParseQueryString(ByVal queryString As String) As WebHeaderCollection
            Dim res = New WebHeaderCollection()
            Dim num As Integer = If(queryString IsNot Nothing, queryString.Length, 0)
            For i As Integer = 0 To num - 1
                Dim startIndex As Integer = i
                Dim num4 As Integer = -1
                Do While i < num
                    Dim ch As Char = queryString.Chars(i)
                    If ch = "="c Then
                        If num4 < 0 Then
                            num4 = i
                        End If
                    ElseIf ch = "&"c Then
                        Exit Do
                    End If

                    i += 1
                Loop

                Dim str = String.Empty
                Dim str2 = String.Empty
                If num4 >= 0 Then
                    str = queryString.Substring(startIndex, num4 - startIndex)
                    str2 = queryString.Substring(num4 + 1, (i - num4) - 1)
                Else
                    str2 = queryString.Substring(startIndex, i - startIndex)
                End If

                res(str.Replace("?", String.Empty)) = HttpUtility.UrlDecode(str2)
            Next i

            Return res
        End Function

        Private Sub OnRegisterButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If Me.ViewModel.IsRegisterEnabled Then
                Me.ViewModel.Register(Sub()
                                          MessageBox.Show(String.Format(CultureInfo.CurrentCulture, "User {0} successfully registered.", Me.ViewModel.UserName), "Registration Successful", MessageBoxButton.OK)

                                          SetIsolatedStorageSetting("UserIsRegistered", True)
                                          Me.NavigationService.GoBack()
                                      End Sub,
                                      Sub(msg) MessageBox.Show(msg, "Registration Error", MessageBoxButton.OK))
            Else
                MessageBox.Show("Please fill out all the information in the registration page.", "Registration Incomplete", MessageBoxButton.OK)
            End If
        End Sub

        Private Sub OnTextBoxKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.Key = Key.Enter Then
                If sender Is Me.UserNameTextBox Then
                    Me.EMailTextBox.Focus()
                Else
                    Me.RegisterButton.Focus()
                End If
            End If
        End Sub
    End Class
End Namespace